<template>
    <div>
        <saveRefund @getfindDeta="getfindDeta"></saveRefund>
        <el-button type="info" @click="updates" icon="el-icon-wallet">{{$t('m.button.bulkRefund')}}</el-button>
        <download style="margin:0" :tableData="tableData"></download>
        <pagination :length="tableData.length" :pagination="pagination" float="right"></pagination>
        <el-table
            :data="tableData.slice((pagination.currpage - 1) * pagination.pagesize, pagination.currpage * pagination.pagesize)"
            :header-cell-style="getRowClass"
            v-loading="loading"
            border
            @selection-change="handleSelectionChange"
            style="width: 100%"
        >
            <el-table-column type="selection" width="55" :selectable="handleDisable"></el-table-column>
            <el-table-column prop="brandName" :label="$t('m.label.account')"></el-table-column>
            <el-table-column prop="countryName" :label="$t('m.label.country')" width="70"></el-table-column>
            <el-table-column prop="amzOrderId" label="Orader ID"></el-table-column>
            <el-table-column prop="productModelNumber" :label="$t('m.label.productModelNumber')"></el-table-column>
            <el-table-column prop="amzRefundAmount" :label="$t('m.label.refundAmount')" width="100"></el-table-column>
            <el-table-column prop="cDate" :label="$t('m.label.creationTime')"></el-table-column>
            <el-table-column prop="cUser" :label="$t('m.label.founder')"></el-table-column>
            <el-table-column :label="$t('m.label.operating')" width="160">
                <template slot-scope="scope">
                    <el-button
                        :type="scope.row.status == 1?'info': scope.row.status == 2?'primary':''"
                        size="mini"
                        @click="update(scope.row)"
                        :disabled="scope.row.status === 1"
                    >{{scope.row.status |status}}</el-button>
                    <el-button
                        type="danger"
                        size="mini"
                        :disabled="scope.row.status !== 2"
                        @click="deleteItem(scope.row)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination :length="tableData.length" :pagination="pagination" float="right"></pagination>
    </div>
</template>
<script>
const ERR_OK = 200;
import pagination from "components/pagination/pagination.vue";
import saveRefund from "./refund-saveRefund.vue";
import download from "./refund-download.vue";
export default {
    props: ["formData"],
    data() {
        return {
            loading: true,
            tableData: [],
            ERR_URL: "",
            pagination: {
                pagesize: 20,
                currpage: 1
            },
            multipleSelection: []
        };
    },
    filters: {
        status(string) {
            return string == 1 ? this.$t('m.button.refunded') : string == 2 ?this.$t('m.button.pending') : this.$t('m.button.other');
        }
    },
    created() {
        this.getfindDeta();
    },
    methods: {
        getfindDeta() {
            this.loading = true;
            this.tableData = [];
            this.$ajax
                .post(
                    this.GLOBAL.reviewMonitoringService +
                        "/refund/find" +
                        this.ERR_URL
                )
                .then(res => {
                    if (res.data.code) {
                        if (res.data.data.data instanceof Array) {
                            this.tableData = res.data.data.data;
                        }
                    }
                    this.loading = false;
                });
        },
        // 全选
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 函数需要一个返回值,true为可选,false为不可选择
        handleDisable(row, index) {
            if (row.status === 2) {
                return true;
            } else {
                return false;
            }
        },
        //批量退款
        updates() {
            if (this.multipleSelection.length > 0) {
                this.$confirm(this.$t('m.message.confirmSubmitARefund'), this.$t('m.message.prompt'), {
                    confirmButtonText: this.$t('m.button.determine'),
                    cancelButtonText: this.$t('m.button.cancel'),
                    type: "warning"
                })
                    .then(() => {
                        let multipleSelection = JSON.parse(
                            JSON.stringify(this.multipleSelection)
                        );
                        multipleSelection.forEach(item => {
                            item.status = 1;
                        });
                        this.$ajax({
                            method: "post",
                            url:
                                this.GLOBAL.reviewMonitoringService +
                                "/refund/updates",
                            data: multipleSelection
                        }).then(res => {
                            if (res.data.code === ERR_OK) {
                                this.multipleSelection.forEach(item => {
                                    item.status = 1;
                                });
                                this.$message.success(this.$t('m.message.submitARefund'));
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        });
                    })
                    .catch(() => {});
            } else {
                this.$message.warning(this.$t('m.message.messageSubmitARefund'));
            }
        },
        //待处理
        update(item) {
            if (item.status === 2) {
                this.$confirm(this.$t('m.message.ConfirmSubmitARefund'), this.$t('m.message.prompt'), {
                    confirmButtonText: this.$t('m.button.determine'),
                    cancelButtonText: this.$t('m.button.cancel'),
                    type: "warning"
                })
                    .then(() => {
                        let items = JSON.parse(JSON.stringify(item));
                        items.status = 1;
                        this.$ajax({
                            method: "post",
                            url:
                                this.GLOBAL.reviewMonitoringService +
                                "/refund/update",
                            data: items
                        }).then(res => {
                            if (res.data.code === ERR_OK) {
                                item.status = 1;
                                this.$message.success(this.$t('m.message.SubmitARefund'));
                            } else {
                                this.$message.error(res.data.msg);
                            }
                        });
                    })
                    .catch(() => {});
            }
        },
        //删除
        deleteItem(item) {
            this.$ajax
                .post(
                    this.GLOBAL.reviewMonitoringService +
                        "/refund/delete?amzRefundRecordId=" +
                        item.amzRefundRecordId
                )
                .then(res => {
                    if (res.data.code === ERR_OK) {
                        this.tableData.splice(
                            this.tableData.findIndex(
                                items =>
                                    items.amzRefundRecordId ===
                                    item.amzRefundRecordId
                            ),
                            1
                        );
                        this.$message.success(this.$t('m.message.successfullyDeleted'));
                    } else {
                        this.$message.success(res.data.msg);
                    }
                });
        },
        // 标题背景颜色
        getRowClass({ row, column, rowIndex, columnIndex }) {
            if (rowIndex === 0) {
                return "background: #EBEEF5 ";
            } else {
                return "";
            }
        }
    },
    watch: {
        formData(val) {
            this.ERR_URL = "?";
            for (let item in val) {
                if (val[item].length > 0) {
                    if (item === "time") {
                        this.ERR_URL +=
                            "startTime" +
                            "=" +
                            val[item][0] +
                            "&" +
                            "endTime" +
                            "=" +
                            val[item][1] +
                            "&";
                    } else {
                        this.ERR_URL +=
                            item + "=" + this.formData[item].join() + "&";
                    }
                }
            }
            this.getfindDeta();
        }
    },
    components: {
        pagination,
        saveRefund,
        download
    }
};
</script>
<style lang="stylus" scoped>
.el-table {
    margin-top: 20px;
}
</style>
